//
//  SettingController.m
//  GKBHouse
//
//  Created by  on 12-7-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "SettingController.h"
#import "GKBNavigationBar.h"
#import "SystemSettting.h"
#import "GKBMainController.h"
#import "ChooseScene.h"
#import "AppFile.h"
#import "TimingSetViewController.h"

static BOOL haveTimeSetting = NO;

@implementation SettingController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initTopBar];
    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]]];
    [systemSet setTitle:NSLocalizedString(@"systemSetting", nil) forState:UIControlStateNormal];
    [systemSet addTarget:self action:@selector(systemMenu) forControlEvents:UIControlEventTouchUpInside];
    [sceneSet setTitle:NSLocalizedString(@"sceneSetting", nil) forState:UIControlStateNormal];
    [sceneSet addTarget:self action:@selector(sceneMenu) forControlEvents:UIControlEventTouchUpInside];
    
    [timingSet setTitle:NSLocalizedString(@"timingSet", nil) forState:UIControlStateNormal];
    [timingSet addTarget:self action:@selector(timerMenu) forControlEvents:UIControlEventTouchUpInside];
    
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

//- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
//{
//    // Return YES for supported orientations
//    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
//}
- (void)initTopBar
{    
    GKBNavigationBar *topBar = [[GKBNavigationBar alloc]init];
    [topBar setTarget:self returnAction:@selector(dismissView) withTitle:NSLocalizedString(@"set", nil)];
    [self.view addSubview:topBar];    
    [topBar release];
    
}
- (void)dismissView
{
    [self dismissModalViewControllerAnimated:NO];
    [GKBMainController postStartRefresh];
}
- (void)systemMenu
{
    SystemSettting *systemSetting = [[SystemSettting alloc]init];
    [self presentModalViewController:systemSetting animated:NO];
    [systemSetting release];
}
- (void)sceneMenu
{
    if ([LoginController1 loginType] == LoginTypeLocal) {
        //本地登录，无场景配置文伯
        NSString *sceneFile = [AppFile sceneFile];
        if (!sceneFile) {
            NSString *noSceneTip = NSLocalizedString(@"no set local user", nil);
            [iToast showTip:noSceneTip gravity:iToastGravityCenter];
            return;//
        }
    }
    
    
    ChooseScene *chooseScene = [[ChooseScene alloc]init];
    
    [self presentModalViewController:chooseScene animated:NO];
    [chooseScene release];
}

- (void)timerMenu
{
    if ([SettingController haveTimeSetting] == NO) {//此主机，没有定时功能
        NSString *noTimeTip = NSLocalizedString(@"Timing-no-timeset", nil);
        [iToast showTip:noTimeTip gravity:iToastGravityCenter];
        return;
    }
    TimingSetViewController *timingSetVC = [[TimingSetViewController alloc]initWithNibName:@"TimingSetViewController" bundle:nil];
    [self presentModalViewController:timingSetVC animated:NO];
    [timingSetVC release];
}

+ (void)setHaveTimeSetting:(BOOL)have
{
    haveTimeSetting = have;
}
+ (BOOL)haveTimeSetting
{
    return haveTimeSetting;
}

@end
